package whf.framework.security.acegiimp;

import java.util.Set;

import org.acegisecurity.GrantedAuthority;
import org.acegisecurity.userdetails.UserDetails;

import whf.framework.security.entity.Role;
import whf.framework.security.entity.User;

/**
 * 用户详细,包括角色信息
 * @author wanghaifeng
 * 
 */
public class UserDetailsImp implements UserDetails {
	private User user;
	
	private Set<Role> roles;
	
	private GrantedAuthority[] gas;

	public UserDetailsImp(User user, Set<Role> roles) throws AcegiException {
		this.user = user;
		this.roles = roles;
		//
		gas = new GrantedAuthority[roles.size()];
		int count = 0;
		for (Role r : roles) {
			GrantedAuthorityImp ga = new GrantedAuthorityImp(r);
			gas[count] = ga;
			count ++;
		}
	}

	/**
	 * @modify wanghaifeng Oct 12, 2006 3:42:28 PM
	 * @return
	 */
	public User getUser(){
		return this.user;
	}
	
	/**
	 * @modify wanghaifeng Oct 12, 2006 3:42:31 PM
	 * @return
	 */
	public Set<Role> getRoles(){
		return this.roles;
	}
	
	/* (non-Javadoc)
	 * @see org.acegisecurity.userdetails.UserDetails#getPassword()
	 */
	public String getPassword() {
		return user.getPassword();
	}

	/* (non-Javadoc)
	 * @see org.acegisecurity.userdetails.UserDetails#getUsername()
	 */
	public String getUsername() {
		return user.getUsername();
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#hashCode()
	 */
	public int hashCode() {
		return user.hashCode();
	}

	/* (non-Javadoc)
	 * @see org.acegisecurity.userdetails.UserDetails#isAccountNonExpired()
	 */
	public boolean isAccountNonExpired() {
		return user.isAccountNonExpired();
	}

	/* (non-Javadoc)
	 * @see org.acegisecurity.userdetails.UserDetails#isAccountNonLocked()
	 */
	public boolean isAccountNonLocked() {
		return user.isAccountNonLocked();
	}

	/* (non-Javadoc)
	 * @see org.acegisecurity.userdetails.UserDetails#isCredentialsNonExpired()
	 */
	public boolean isCredentialsNonExpired() {
		return user.isCredentialsNonExpired();
	}

	/* (non-Javadoc)
	 * @see org.acegisecurity.userdetails.UserDetails#isEnabled()
	 */
	public boolean isEnabled() {
		return user.isEnabled();
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	public String toString() {
		return user.toString();
	}

	/* 
	 * 返回该用户所有的授权,系统中相当于角色
	 * @see org.acegisecurity.userdetails.UserDetails#getAuthorities()
	 */
	public GrantedAuthority[] getAuthorities() {
		return gas;
	}

}
